package com.example.spring_java_oj.controller;

import com.example.spring_java_oj.Util.LoginUtil;
import com.example.spring_java_oj.model.Problem;
import com.example.spring_java_oj.model.Result;
import com.example.spring_java_oj.service.ProblemAmountService;
import com.example.spring_java_oj.service.userInfo.UserInfoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/problem")
public class problemController {
//    @Autowired
//    private ProblemService problemService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private ProblemAmountService problemAmountService;


    /**
     * 获得题目列表
     * @return
     */
    @RequestMapping("/getProblems")
    public List<Problem> getList(HttpServletRequest request) throws JsonProcessingException {
        if(!LoginUtil.login(request, userInfoService)){
            return null;
        }
        //return problemService.getList();
        List<Problem> problems = problemAmountService.getList();
        if (problems == null){
            return new ArrayList<>();
        }
        return problems;
    }

    /**
     * 获得题目详细情况
     * @param id
     * @return
     */
    @RequestMapping("/getProblem")
    public Problem getProblem(Integer id, HttpServletRequest request) throws JsonProcessingException {
        if(!LoginUtil.login(request, userInfoService)){
            return null;
        }
        //Problem problem = problemService.getProblem(id, request);
        Problem problem = problemAmountService.getProblem(id, request);
        if (problem == null){
            return new Problem();
        }
        return problem;
    }

    /**
     * 修改题目
     * @param problem
     * @return
     */
    @RequestMapping("/updateProblem")
    public Result updateProblem(Problem problem, HttpServletRequest request) throws JsonProcessingException {
        if(!LoginUtil.login(request, userInfoService)){
            return null;
        }
        System.out.println(problem.toString());
        //int n = problemService.updateProblem(problem);
        int n = problemAmountService.updateProblem(problem);
        if (n == 1){
            return Result.success(true);
        }else if(n == 2){
            return Result.success(true);
        }
        return Result.fail("修改失败", false);
    }

    /**
     * 删除题目
     * @param id
     * @return
     */
    @RequestMapping("/deleteProblem")
    public Result deleteProblem(Integer id, HttpServletRequest request){
        if(!LoginUtil.login(request, userInfoService)){
            return null;
        }
        //int n = problemService.deleteProblem(id);
        int n = problemAmountService.deleteProblem(id);
        if (n == 1){
            return Result.success(true);
        }
        return Result.fail("删除失败", false);
    }

    /**
     * 添加题目
     * @param problem
     * @return
     */
    @RequestMapping("/add")
    public Result addProblem(Problem problem, HttpServletRequest request) throws JsonProcessingException {
        if(!LoginUtil.login(request, userInfoService)){
            return null;
        }
        //int n = problemService.insert(problem);
        int n = problemAmountService.insert(problem);
        System.out.println(problem.toString());
        if (n == 1){
            return Result.success(true);
        }
        return Result.fail("添加失败", false);
    }

//    @RequestMapping("/revive")
//    public String revive(Integer id, HttpServletRequest request){
//        if(!LoginUtil.login(request, userInfoService)){
//            return null;
//        }
//        System.out.println(id);
//        Problem problem = problemService.getProblemStart(id);
//        System.out.println(problem.toString());
//        return problem.getTemplateCode();
//    }

    @RequestMapping("/getProblemStart")
    public Problem getProblemStart(Integer id, HttpServletRequest request) throws JsonProcessingException {
        if(!LoginUtil.login(request, userInfoService)){
            return null;
        }
        Problem problem = problemAmountService.getProblemStart(id);
        return problem;
    }

    /**
     * 获取样例代码(用户第一次时在代码框里看到的)
     * @return Problem
     * @throws IOException
     */
    @RequestMapping("/TemAndTestCode")
    public Problem getTemCode() throws IOException {
        return problemAmountService.getTemCode();
    }

}
